using System;
using Impact.Model.Objects;
using Impact.Model.Service;
using StructureMap;
using Impact.Views;

namespace Impact.Controllers
{
    /// <summary>
    /// Controller for event-related logic
    /// </summary>
    public class EventController
    {
        private IEventEditorView view;
        private Guid userId;
        private IEventService service;

        /// <summary>
        /// default constructor
        /// </summary>
        public EventController( IEventEditorView view, Guid userId )
        {
            this.view = view;
            this.userId = userId;
            this.service = ObjectFactory.GetInstance<IEventService>();
        }

        /// <summary>
        /// setup the editor in the view 
        /// </summary>
        public void InitializeEditor( bool isFirstTime )
        {

            if ( isFirstTime )
            {
                Event evt = null;
                if ( CurrentEventId != null )
                {
                    // if pageId is valid, we're editing.
                    evt = service.Get( CurrentEventId.Value.ToString() );
                }
                else
                {
                    // otherwise, we're adding.
                    evt = new Event();
                }
                view.PopulateForm( evt );

            }

        }

        /// <summary>
        /// store the event data from the view
        /// </summary>
        public void SaveEvent()
        {
            service.Save( view.RetrieveFormData(), this.userId );
        }

        /// <summary>
        /// gets the id of the event currently being edited.
        /// </summary>
        protected int? CurrentEventId
        {
            get
            {
                string eventIdParam = view.Parameters["id"];
                int eventId = -1;
                int? output = null;
                if ( Int32.TryParse( eventIdParam, out eventId ) )
                {
                    output = eventId;
                }
                return output;
            }
        }


        #region Static Members


        /// <summary>
        /// retrieve all events for public display
        /// </summary>
        public static EventCollection GetUpcomingEvents()
        {
            IEventService service = ObjectFactory.GetInstance<IEventService>();
            return service.GetFutureEvents( 10 );
        }

        /// <summary>
        /// retrieve the given event 
        /// </summary>
        public static Event GetEvent( string id )
        {
            IEventService service = ObjectFactory.GetInstance<IEventService>();
            return service.Get( id );
        }

        /// <summary>
        /// retrieve all events in the store
        /// </summary>
        /// <returns></returns>
        public static EventCollection GetAllEvents()
        {
            IEventService service = ObjectFactory.GetInstance<IEventService>();
            return service.GetAll( false );
        }

        /// <summary>
        /// remove the given event
        /// </summary>
        public static void DeleteEvent( string eventId )
        {
            IEventService service = ObjectFactory.GetInstance<IEventService>();
            service.Delete( eventId );
        }

        /// <summary>
        /// flip the 'enabled' state of the given event
        /// </summary>
        public static void ToggleEnabled( string eventId, Guid userId )
        {
            IEventService service = ObjectFactory.GetInstance<IEventService>();
            Event e = Event.FetchByID( eventId );
            e.Enabled = !e.Enabled;
            e.Save( userId );

        }

        #endregion

    }
}
